<?php

namespace app\core;
// function userErrorHandler($errno, $errmsg, $filename, $linenum) 
// {
//     $dt = date("Y-m-d H:i:s");
//     $errortype = array (
//                 E_ERROR              => 'Error',
//                 E_WARNING            => 'Warning',
//                 E_PARSE              => 'Parsing Error',
//                 E_NOTICE             => 'Notice',
//                 E_CORE_ERROR         => 'Core Error',
//                 E_CORE_WARNING       => 'Core Warning',
//                 E_COMPILE_ERROR      => 'Compile Error',
//                 E_COMPILE_WARNING    => 'Compile Warning',
//                 E_USER_ERROR         => 'User Error',
//                 E_USER_WARNING       => 'User Warning',
//                 E_USER_NOTICE        => 'User Notice',
//                 E_STRICT             => 'Runtime Notice',
//                 E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
//                 );
//     // set of errors for which a var trace will be saved
//     $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
//     $err="[$dt] $errno $errortype[$errno] $errmsg $filename $linenum";

//     // if (in_array($errno, $user_errors)) {

//     //     $err.=json_encode($vars,JSON_UNESCAPED_UNICODE);
//     // }
//     error_log($err."\r\n", 3,LOG."error.log");
//     return true;
// }
// set_error_handler("userErrorHandler");
use Swoole\Coroutine;

class HttpError
{
    public static function handdle($errno, $errstr, $errfile, $errline)
    {
        if (!(error_reporting() & $errno)) {
            // This error code is not included in error_reporting, so let it fall
            // through to the standard PHP error handler
            return false;
        }
        $dt = date("Y-m-d");
        $dtError = date("Y-m-d H:i:s");
        // $errstr may need to be escaped:
        // $errstr = htmlspecialchars($errstr);
        $errortype = array(
            E_ERROR              => 'Error',
            E_WARNING            => 'Warning',
            E_PARSE              => 'Parsing Error',
            E_NOTICE             => 'Notice',
            E_CORE_ERROR         => 'Core Error',
            E_CORE_WARNING       => 'Core Warning',
            E_COMPILE_ERROR      => 'Compile Error',
            E_COMPILE_WARNING    => 'Compile Warning',
            E_USER_ERROR         => 'User Error',
            E_USER_WARNING       => 'User Warning',
            E_USER_NOTICE        => 'User Notice',
            E_STRICT             => 'Runtime Notice',
            E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
        );
        $log = "[$dtError][$errortype[$errno]]$errfile $errline ";
        switch ($errno) {
            case E_USER_ERROR:;
                $log .= "$errstr\n";
                // Coroutine::sleep(.001);
                // exit(1);
            case E_USER_WARNING:

                $log .= "$errstr\n";
                break;

            case E_USER_NOTICE:

                $log .= "$errstr\n";
                break;

            default:

                $log .= "$errstr\n";
                break;
        }
        $file = "{$dt}.log";
        // print_r($log);
        // $source=File::open($file);

        // File::write($source,$log);
        // File::close($source);
        error_log($log, 3, LOG . "{$dt}.log");
        /* Don't execute PHP internal error handler */
        return true;
    }
}
